<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text"><span>请输入用户名</span>
    <script>
        var reg = /^[a-zA-Z0-9_-]{6,16}$/
        var ipt = document.querySelector('input');
        var span = document.querySelector('span');
        ipt.addEventListener('blur', function () {
            if (reg.test(this.value)) {
                span.innerHTML = '用户名正确';
                span.style.color = 'green';

            } else {
                span.innerHTML = '用户名错误';
                span.style.color = 'red';
            }


        })
    </script>
</body>

</html>